/*
 * $Id: FcDateTimeUtils.java 46785 2011-09-06 02:57:53Z pigeon $
 *
 * Copyright ( c ) 2010 Carefx Corporation. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of Carefx
 * Corporation ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with Carefx Corporation or a Carefx
 * auhorized reseller (the "License Agreement"). Carefx may make changes to the
 * Confidential Information from time to time. Such Confidential Information may
 * contain errors.
 *
 * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, CAREFX DISCLAIMS ALL
 * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH
 * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR
 * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
 * PARTICULAR PURPOSE. CAREFX DOES NOT WARRANT THAT END USER'S USE OF THE
 * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE.
 *
 * CAREFX SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR
 * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL
 * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN
 * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF CAREFX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES. CAREFX' TOTAL LIABILITY TO END USER SHALL NOT
 * EXCEED THE AMOUNTS PAID FOR THE CAREFX SOFTWARE BY END USER DURING THE PRIOR
 * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE.  BECAUSE SOME
 * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER.
 *
 * Copyright version 2.0
 */
package com.th.supcom.pds.impl.common.utils;

import java.util.Date;

import net.carefx.fc.framework.web.gwt.util.FcConstants;

import com.google.gwt.i18n.client.DateTimeFormat;

/**
 * 日期/时间相关操作工具类.
 * 
 * @author jtang
 * @version 1.0.0 2010-12-3
 */
public final class DmsDateTimeUtils
{
    public static final long MILLISECONDS_OF_DAY = 24 * 60 * 60 * 1000;

    /**
     * 无参构造函数.
     */
    private DmsDateTimeUtils ()
    {

    }

    /**
     * 获得当前日期时间
     */
    public static Date getCurrentDate ()
    {
        return new Date ();
    }

    // public static Date getFirstDay ()
    // {
    // Date d = getCurrentDate ();
    // d.setDate (1);
    // return d;
    // }

    /**
     * 获得当前日期时间
     */
    public static int getYear ()
    {
        return getCurrentDate ().getYear () + 1900;
    }

    // /**
    // * 两个日期相差的天数.
    // *
    // * @return 统计开始日期
    // */
    // public static Date getStartDate ()
    // {
    // Date date = new Date ();
    // date.setDate (1);
    // return date;
    // }

    // /**
    // * 两个日期相差的天数.
    // *
    // * @return 统计开始日期
    // */
    // public static Date getEndDate ()
    // {
    // return new Date ();
    // }

    /**
     * 初始化统计日期时间值.
     * 
     * @return 日期
     */
    public static Date initStartDateTime (Date date)
    {
        date.setHours (0);
        date.setMinutes (0);
        date.setSeconds (0);
        return date;
    }

    /**
     * 初始化统计日期时间值.
     * 
     * @return 日期
     */
    public static Date initEndDateTime (Date date)
    {
        date.setHours (23);
        date.setMinutes (59);
        date.setSeconds (59);
        return date;
    }

    // /**
    // * 两个日期相差的天数.
    // *
    // * @param date1 之前日期
    // * @param date2 之后日期
    // * @return 天数
    // */
    // public static int getDayDifference (Date date1, Date date2)
    // {
    // return (int) ((date2.getTime () - date1.getTime ()) /
    // MILLISECONDS_OF_DAY);
    // }

    /**
     * 比较两个日期相隔的天数 (只要)
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static int getIntervalDay (Date date1, Date date2)
    {
        DateTimeFormat dtf = DateTimeFormat.getFormat (FcConstants.DATE_YMD_FORMAT);
        Date date1Copy = dtf.parse (dtf.format ((Date) date1.clone ()));
        Date date2Copy = dtf.parse (dtf.format ((Date) date2.clone ()));

        long day = (date2Copy.getTime () - date1Copy.getTime ()) / MILLISECONDS_OF_DAY;
        return (int) day;
    }

    /**
     * 两个日期相差的天数.
     * 
     * @param date1 之前日期
     * @param 天数
     * @return date2 之后日期
     */
    public static Date getDateAddDay (Date date1, int day)
    {
        return new Date (date1.getTime () + (day * MILLISECONDS_OF_DAY));
    }

    // /**
    // * 将Date类型用DateFormat.MEDIUM格式格式化成日期+时间形式的字符串.
    // *
    // * @param date 格式化前日期
    // * @return 格式化后字符串
    // */
    // @SuppressWarnings ("deprecation")
    // public static String format2DateTimeStr (Date date)
    // {
    // if (date == null)
    // {
    // return null;
    // }
    // int year = date.getYear () + 1900;
    // int month = date.getMonth () + 1;
    // int day = date.getDate ();
    // int hours = date.getHours ();
    // int minutes = date.getMinutes ();
    // int seconds = date.getSeconds ();
    // return year + "-" + (month < 10 ? "0" : "") + month + "-" + (day < 10 ?
    // "0" : "") + day + " " + (hours < 10 ? "0" : "") + hours
    // + ":" + (minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" :
    // "") + seconds;
    // }

    // /**
    // * 将Date类型用DateFormat.MEDIUM格式格式化成日期+时间形式的字符串(yyyy-MM-dd HH:mm).
    // *
    // * @param date 格式化前日期
    // * @return 格式化后字符串
    // */
    // @SuppressWarnings ("deprecation")
    // public static String format2ShortDateTimeStr (Date date)
    // {
    // if (date == null)
    // {
    // return null;
    // }
    // int year = date.getYear () + 1900;
    // int month = date.getMonth () + 1;
    // int day = date.getDate ();
    // int hours = date.getHours ();
    // int minutes = date.getMinutes ();
    // return year + "-" + (month < 10 ? "0" : "") + month + "-" + (day < 10 ?
    // "0" : "") + day + " " + (hours < 10 ? "0" : "") + hours
    // + ":" + (minutes < 10 ? "0" : "") + minutes;
    // }

    /**
     * 将Date类型用DateFormat.MEDIUM格式格式化成日期+时间形式的字符串(yyyy年MM月dd日 HH时mm分).
     * 
     * @param date 格式化前日期
     * @return 格式化后字符串
     */
    // @SuppressWarnings ("deprecation")
    // public static String format2ShortCNDateTimeStr (Date date)
    // {
    // if (date == null)
    // {
    // return null;
    // }
    // int year = date.getYear () + 1900;
    // int month = date.getMonth () + 1;
    // int day = date.getDate ();
    // int hours = date.getHours ();
    // int minutes = date.getMinutes ();
    // return year + "年" + month + "月" + +day + "日 " + +hours + "时" + +minutes +
    // "分";
    // }

    /**
     * 将Date类型用DateFormat.MEDIUM格式格式化成日期形式的字符串.
     * 
     * @param date 格式化前日期
     * @return 格式化后字符串
     */
    // @SuppressWarnings ("deprecation")
    // public static String format2DateStr (Date date)
    // {
    // if (date == null)
    // {
    // return null;
    // }
    // int year = 1900 + date.getYear ();
    // int month = date.getMonth () + 1;
    // int day = date.getDate ();
    // return year + "-" + (month < 10 ? "0" : "") + month + "-" + (day < 10 ?
    // "0" : "") + day;
    // }

    /**
     * 将DateFormat.MEDIUM格式的日期+时间形式的字符串解析成Date类型.
     * 
     * @param dateTimeStr 格式化前日期字符串
     * 
     * @return 转换后日期
     */
    @SuppressWarnings ("deprecation")
    public static Date parseFromDateTimeStr (String dateTimeStr)
    {
        Date date = null;
        try
        {
            String[] datePart = dateTimeStr.split (" ");
            date = new Date (datePart[0].replaceAll ("-", "/"));
            String[] timePart = datePart[1].split (":");
            date.setHours (Integer.parseInt (timePart[0]));
            date.setMinutes (Integer.parseInt (timePart[1]));
            date.setSeconds (Integer.parseInt (timePart[2]));
        }
        catch (Exception e)
        {
            e.printStackTrace ();
        }
        return date;
    }

    /**
     * 判断参数日期是否为闰年
     * 
     * @param year 需要判定是否为闰年的年份
     * @return 布尔值
     */
    public static boolean isLeapYear (int year)
    {

        if (year % 400 == 0 || ((year % 4 == 0) && (year % 100 != 0)))
        {
            return true;
        }
        return false;
    }

    public static Date getDate (int mouth)
    {
        Date date = new Date ();
        date.setMonth (date.getMonth () + mouth);
        if (date.getMonth () > 12 - mouth)
        {
            date.setYear (date.getYear () + 1);
            date.setMonth (date.getMonth () + mouth - 12);
        }
        return date;
    }


    /** 公司版权信息. */
    @SuppressWarnings ("unused")
    private static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
